package cn.singno.bob.jpa.bean;

import java.io.Serializable;
import java.util.List;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import com.google.common.collect.Lists;

/**
 * 数据库帮助类
 * @author bob
 *
 */
public class SQLHelper {

	private StringBuffer sql; //SQL语句
	private List<Object> params = Lists.newArrayList(); //参数
	private String defaultAlias = "o";			//默认别名
	
	/**
	 * 构造一
	 * @param sql
	 */
	public SQLHelper(String sql){
		this.sql = new StringBuffer(sql);
	}
	
	public SQLHelper(StringBuffer sql){
		this.sql = sql;
	}
	
	/**
	 * 构造二
	 * @param clazz
	 */
	public SQLHelper (Class<?> clazz){
		this.sql = new StringBuffer("select " + this.defaultAlias + " from " + clazz.getSimpleName() + this.defaultAlias + " where 1 = 1 ");
	}
	
	/**
	 * 构造三
	 * @param clazz
	 * @param alias 别名
	 */
	public SQLHelper(Class<?> clazz, String alias){
		this.defaultAlias = alias ;
		this.sql = new StringBuffer("select " + this.defaultAlias + " from " + clazz.getSimpleName() + this.defaultAlias + "where 1 = 1 ");
	}
	
	/**
	 * 添加排序
	 * @param propertyName 字段名
	 * @param isAsc true :asc false:desc
	 * @return
	 */
	public SQLHelper addOrder(String propertyName, boolean isAsc){
		this.sql.append(" order by " + this.defaultAlias + "." + propertyName + (isAsc ? " asc" : " desc"));
		return this;
	}
	
	/**
	 * 添加自定义排序
	 * @param oderbySql 全的排序SQL: order by o.createtime desc
	 * @return
	 */
	public SQLHelper addOrder(String oderbySql){
		this.sql.append(oderbySql);
		return this;
	}
	
	/**
	 * 自定义添加的语句和参数，将不判断里面参数的合法性
	 * @param custom : o.name = ?
	 * @param param : "李四"
	 * @return
	 */
	public SQLHelper addCustom(String custom, Serializable param){
		this.sql.append(custom);
		this.params.add(param);
		return this;
	}
	
	/**
	 * 
	 * <p>描述：直接添加参数</p>
	 * <pre>
	 *    
	 * </pre>
	 * @param params
	 * @return
	 */
	public SQLHelper addParam(Serializable... params){
		if(ArrayUtils.isNotEmpty(params)){
			for (Serializable param : params) {
				this.params.add(param);
			}
		}
		return this;
	}
	

	/**
	 * 添加一组条件
	 * @param conditions
	 * @param params
	 * @return
	 */
	public SQLHelper addCondition(String[] conditions, Serializable[] params){
		for (int i = 0; i < params.length && i < conditions.length; i++) {
			addCondition(conditions[i], params[i]);
		}
		return this;
	}
	/**
	 * 添加一个条件
	 * @param condition
	 * @param param
	 * @return
	 */
	public SQLHelper addCondition(String condition, Serializable param){
		if(param instanceof String){
			if(StringUtils.isNotBlank(param.toString())){
				this.sql.append(condition);
				this.params.add(param);
			}
		}else if(param != null){
			this.sql.append(condition);
			this.params.add(param);
		}
		return this;
	}
	
	/**
	 * 添加between...and..
	 * @param conditions
	 * @param params
	 * @return
	 */
	public SQLHelper addBetweenAnd(String conditions, Serializable params[]){
		if(ArrayUtils.isEmpty(params) || params.length != 2){
			return this;
		}
		this.sql.append(conditions);
		for (Serializable serializable : params) {
			this.params.add(serializable);
		}
		return this;
	}
	
	/**
	 * 
	 * <p>描述：sql.append</p>
	 * <pre>
	 *    
	 * </pre>
	 * @param sql
	 * @return
	 */
	public SQLHelper append(String sql){
		this.sql.append(sql);
		return this;
	}
	

	/**
	 * 获取sql
	 */
	public String toString() {
		return sql.toString();
	}

	/**
	 * 获取参数
	 * @return
	 */
	public Object[] toParams() {
		return params.toArray();
	}
	
}
